﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using System.Collections;
using System.Text.RegularExpressions;
namespace BYManageInfoSystem
{
    public partial class Frm_FeeBGTruePayTab : DockContent
    {
        private string m_strBianHaoTemp = "";
        private bool m_bClearing = false;
        private bool m_bIsPay = true;
        private InsertType m_InsertType = new InsertType();
        private DGBY.BLL.DG_FeeLockTab m_bllDG_FeeLockTab = new DGBY.BLL.DG_FeeLockTab();
        private DGBY.BLL.DG_FeeTruePayLockTab m_bllDG_FeeTruePayLockTab = new DGBY.BLL.DG_FeeTruePayLockTab();
        private readonly DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        private DGBY.BLL.FeeBGTruePayTab m_bllFeeBGTruePayTab = new DGBY.BLL.FeeBGTruePayTab();
        private bool m_bInsertFlag = false;
        private int m_nDelIndex = -1;
        private DGBY.Model.FeeBGTruePayTab m_modelFeeBGTruePayTab = new DGBY.Model.FeeBGTruePayTab();
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private DGBY.BLL.BGHRecvPeoTelTab m_bllBGHRecvPeoTelTab = new DGBY.BLL.BGHRecvPeoTelTab();
        private DGBY.BLL.BGHarborTab m_bllBGHarborTab = new DGBY.BLL.BGHarborTab();
        private DGBY.BLL.FeeBGNoContractNoAndFacTab m_FeeBGNoContractNoAndFacTab = new DGBY.BLL.FeeBGNoContractNoAndFacTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();

        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        Hashtable m_SQLStringList = new Hashtable();
        ArrayList m_arrayList = new ArrayList();

        public Frm_FeeBGTruePayTab()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView(bool bIsPay)
        {
            string strTemp = this.comboBoxShowWay.Text;
            if (strTemp == "全部记录")
                ShowAllRecord("", "", bIsPay);
            else
            {
                DateTime dtNow = System.DateTime.Now.Date.AddDays(-180);
                ShowAllRecord("case when year(HxdNoTab.SBDate) = 1900 then getdate() else HxdNoTab.SBDate end >= '" + dtNow + "' or FeeBGTruePayTab.PaidanRegisterTabid = -1", "", bIsPay);
            }
        }

        private void ShowAllRecord(string strWhere1, string strWhere2, bool bIsPay)
        {
            m_bIsPay = bIsPay;
            DataSet ds = m_bllFeeBGTruePayTab.GetList(strWhere1, strWhere2);
            ultraGrid1.DataSource = null;

            ultraGrid1.DataSource = ds.Tables[0];
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;

            ultraGrid1.DisplayLayout.Bands[0].Columns["FeeID"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["PaidanID"].Hidden = true;
            if (m_bIsPay)
            {
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收通关单费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收报关费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收港口建设费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收GPS费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收连柜费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收加页费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收查柜费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收改船费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收扫描费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收茶水费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收放行后查柜费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收入仓代垫费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收其他费用"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额"].Hidden = true;
                
            }
            else
            {
                ultraGrid1.DisplayLayout.Bands[0].Columns["通关单费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["报关费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["港口建设费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["GPS费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["连柜费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["加页费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["查柜费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["改船费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["扫描费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["茶水费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["放行后查柜费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["入仓代垫费"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["其他费用"].Hidden = true;
            }
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已同步费用"].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Hidden = true;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns["转关报关行"].ValueList = this.ultraDropDownBGH;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["转关报关港"].ValueList = this.ultraDropDownBGPort;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].ValueList = this.ultraDropDownComp;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].ValueList = this.ultraDropDownDep;
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView(true);
        }

        private void InitComBox()
        {
            this.comboBoxShowWay.Items.AddRange(new object[] { "最近半年记录", "全部记录" });
            this.comboBoxShowWay.SelectedIndex = 0;

            DataSet ds = m_bllBGHDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBGH.DataSource = ds.Tables[0];

                this.comboBoxBGH.ValueMember = "BGHDataTabID";
                this.comboBoxBGH.DisplayMember = "报关行简称";
                this.comboBoxBGH.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxBGH.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxBGH.SelectedIndex = 0;
            }

        }

        private void InitUltraDropDown()
        {
            DataSet dsTemp = m_bllBGHDataTab.GetList("");

            this.ultraDropDownBGH.DataSource = dsTemp.Tables[0];
            this.ultraDropDownBGH.DisplayMember = "报关行简称";
            this.ultraDropDownBGH.ValueMember = "BGHDataTabID";
            this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownBGH.DisplayLayout.Bands[0].Columns["报关行简称"].Hidden = false;

            dsTemp = m_bllBGHarborTab.GetList("");

            this.ultraDropDownBGPort.DataSource = dsTemp.Tables[0];
            this.ultraDropDownBGPort.DisplayMember = "报关港";
            this.ultraDropDownBGPort.ValueMember = "BGHarborTabID";
            this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns["报关港"].Hidden = false;

            dsTemp = m_bllBYDepartTab.GetList("");

            this.ultraDropDownDep.DataSource = dsTemp.Tables[0];
            this.ultraDropDownDep.DisplayMember = "部门名称";
            this.ultraDropDownDep.ValueMember = "BYDepartTabID";
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownDep.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns["部门名称"].Hidden = false;

            dsTemp = m_bllBYCompanyTab.GetList("");

            this.ultraDropDownComp.DataSource = dsTemp.Tables[0];
            this.ultraDropDownComp.DisplayMember = "经营单位简称";
            this.ultraDropDownComp.ValueMember = "BYCompanyTabID";
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownComp.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns["经营单位简称"].Hidden = false;
        }

        private void Frm_FeeBGTruePayTab_Load(object sender, EventArgs e)
        {
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
            SyncSBMonth();
            InitComBox();
            InitUltraDropDown();
        }

        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        




        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["编号是否手动生成"].Width = 30;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否直接开票"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["录入日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报年月"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["核销单号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应付实付金额"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公实付金额"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私实付金额"].Width = 60; 
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已核对金额"].Width = 50;

            ultraGrid1.DisplayLayout.Bands[0].Columns["应收其他费用"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收茶水费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收扫描费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收改船费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收GPS费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收查柜费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收加页费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收连柜费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收港口建设费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收报关费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收通关单费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收放行后查柜费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收入仓代垫费"].Width = 50;

            ultraGrid1.DisplayLayout.Bands[0].Columns["其他费用"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["茶水费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["扫描费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["改船费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["GPS费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["查柜费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["加页费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["连柜费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["港口建设费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["通关单费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["放行后查柜费"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["入仓代垫费"].Width = 50;

            ultraGrid1.DisplayLayout.Bands[0].Columns["报关行"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关港"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["转关报关行"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["转关报关港"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经办人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["跟单人员"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂简称"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收费工厂"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私收款人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公收款人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
        }
        private void SetSummary(string strName, InitializeLayoutEventArgs e)
        {
            SummarySettings averageSummary =
e.Layout.Bands[0].Summaries.Add(
strName,
SummaryType.Sum,
e.Layout.Bands[0].Columns[strName]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;
        }
        
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            this.ultraGrid1.DisplayLayout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;
            InitColumnWidth();
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            SetSummary("应付实付金额", e);
            SetSummary("应收金额", e);
            SetSummary("通关单费", e);
            SetSummary("报关费", e);
            SetSummary("港口建设费", e);
            SetSummary("连柜费", e);
            SetSummary("加页费", e);
            SetSummary("GPS费", e);
            SetSummary("查柜费", e);
            SetSummary("改船费", e);
            SetSummary("扫描费", e);
            SetSummary("茶水费", e);
            SetSummary("其他费用", e);
            SetSummary("应收通关单费", e);
            SetSummary("应收报关费", e);
            SetSummary("应收港口建设费", e);
            SetSummary("应收连柜费", e);
            SetSummary("应收加页费", e);
            SetSummary("应收GPS费", e);
            SetSummary("应收查柜费", e);
            SetSummary("应收改船费", e);
            SetSummary("应收扫描费", e);
            SetSummary("应收茶水费", e);
            SetSummary("应收其他费用", e);
            SetSummary("对公实付金额", e);
            SetSummary("对私实付金额", e);
            SetSummary("放行后查柜费", e);
            SetSummary("入仓代垫费", e);
            SetSummary("应收放行后查柜费", e);
            SetSummary("应收入仓代垫费", e);
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "工厂简称" || str == "合同编号" /*|| str == "经营单位"*/
                    /*|| str == "部门"*/ || str == "应付实付金额" || str == "对公收款人"
                    || str == "跟单人员" || str == "报关港" || str == "应收金额"
                    || str == "对公费用编号" || str == "对私费用编号" || str == "报关行"
                    || str == "录入日期" || str == "申报日期" || str == "经办人" || str == "实付日期")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        //if (strTemp == "经营公司" | strTemp == "客户" | strTemp == "币种" | strTemp == "收款方式")
                        //    return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        //if (strTemp == "经营公司" | strTemp == "客户" | strTemp == "币种" | strTemp == "收款方式")
                        //    return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }
        private void ResetRecord(int nFeeID, Boolean bContract)
        {
            try
            {
                if (bContract)
                {
                    DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab
                    = m_bllFeeBGTruePayTab.GetModel(nFeeID);


                    modelFeeBGTruePayTab.ToPubBianHao = "";
                    modelFeeBGTruePayTab.ToPrivateBianHao = "";
                    modelFeeBGTruePayTab.SBDate = DateTime.Parse("1900-01-01");
                    modelFeeBGTruePayTab.TruePayDate = null;
                    modelFeeBGTruePayTab.InputDate = null;
                    modelFeeBGTruePayTab.TruePayMonth = 0;
                    modelFeeBGTruePayTab.ShouldPayMonth = 0;
                    modelFeeBGTruePayTab.PrivateTruePayMoney = 0;
                    modelFeeBGTruePayTab.PubTruePayMoney = 0;

                    modelFeeBGTruePayTab.ToPubFormula = "";
                    modelFeeBGTruePayTab.ToPrivateFormula = "";

                    modelFeeBGTruePayTab.JinBanPerson = "";
                    modelFeeBGTruePayTab.RecvMonyePerson = "";

                    modelFeeBGTruePayTab.IsToRecvPayTab = 0;
                    modelFeeBGTruePayTab.IsToFinCheckMoneyTab = 0;
                    modelFeeBGTruePayTab.Note = "";



                    modelFeeBGTruePayTab.bitFiledReserv1 = false;
                    modelFeeBGTruePayTab.bitFiledReserv2 = false;

                    modelFeeBGTruePayTab.stringFiledReserv1 = "";
                    modelFeeBGTruePayTab.stringFiledReserv2 = "";
                    modelFeeBGTruePayTab.stringFiledReserv3 = "";
                    modelFeeBGTruePayTab.stringFiledReserv4 = "";

                    modelFeeBGTruePayTab.intFiledReserv1 = 33;
                    modelFeeBGTruePayTab.intFiledReserv2 = 10;
                    modelFeeBGTruePayTab.intFiledReserv3 = 0;
                    modelFeeBGTruePayTab.intFiledReserv4 = 0;

                    DateTime dtNow = System.DateTime.Now.Date;
                    modelFeeBGTruePayTab.datetimeFiledReserv1 = dtNow;
                    modelFeeBGTruePayTab.datetimeFiledReserv2 = dtNow;

                    modelFeeBGTruePayTab.decimalFiledReserv1 = 0;
                    modelFeeBGTruePayTab.decimalFiledReserv2 = 0;
                    modelFeeBGTruePayTab.decimalFiledReserv3 = 0;
                    modelFeeBGTruePayTab.decimalFiledReserv4 = 0;

                    m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTab);
                    Frm_Main.Log2DB(this.Text, "清空", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());
                }
                else
                {
                    DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTab
                        = m_FeeBGNoContractNoAndFacTab.GetModel(nFeeID);

                    modelBGNoContractNoAndFacTab.PaidanRegisterTabID = -1;
                    modelBGNoContractNoAndFacTab.bNoManualGeneration = false;
                    modelBGNoContractNoAndFacTab.TruePayDate = null;

                    modelBGNoContractNoAndFacTab.ToPrivateBianHao = "";
                    modelBGNoContractNoAndFacTab.ToPubBianHao = "";

                    modelBGNoContractNoAndFacTab.bDirectlyInvoice = false;
                    modelBGNoContractNoAndFacTab.InputDate = System.DateTime.Now.Date;
                    modelBGNoContractNoAndFacTab.SBDate = null;

                    modelBGNoContractNoAndFacTab.ShouldPayTruePayFee = 0;
                    modelBGNoContractNoAndFacTab.PubTruePayMoney = 0;
                    modelBGNoContractNoAndFacTab.PrivateTruePayMoney = 0;

                    modelBGNoContractNoAndFacTab.bIsChaGui = false;
                    modelBGNoContractNoAndFacTab.OtherFee = 0;
                    modelBGNoContractNoAndFacTab.BGOtherFee = 0;
                    modelBGNoContractNoAndFacTab.ShouldReceiveFee = 0;

                    modelBGNoContractNoAndFacTab.BGHNID = 33;
                    modelBGNoContractNoAndFacTab.BGHarborID = 10;

                    modelBGNoContractNoAndFacTab.JinBanPerson = "";
                    modelBGNoContractNoAndFacTab.BYCompanyTabID = 3;
                    modelBGNoContractNoAndFacTab.BYDepartTabID = 7;

                    modelBGNoContractNoAndFacTab.PubRecvMonyePerson = "";
                    modelBGNoContractNoAndFacTab.PriRecvMonyePerson = "";

                    modelBGNoContractNoAndFacTab.ShouldPayMonth = 0;
                    modelBGNoContractNoAndFacTab.TruePayMonth = 0;

                    modelBGNoContractNoAndFacTab.Note = "";

                    m_FeeBGNoContractNoAndFacTab.Update(modelBGNoContractNoAndFacTab);
                    Frm_Main.Log2DB(this.Text, "清空无合同号工厂记录", modelBGNoContractNoAndFacTab.FeeBGNoContractNoAndFacTabID.ToString());
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }


        private void ResetUltraGridRow(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                CurrentUGR.Cells["对公费用编号"].Value = "";
                CurrentUGR.Cells["对私费用编号"].Value = "";

                CurrentUGR.Cells["实付日期"].Value = DBNull.Value;

                CurrentUGR.Cells["录入日期"].Value = DBNull.Value;

                CurrentUGR.Cells["实付费用计提年月"].Value = 0;
                CurrentUGR.Cells["应收费用计提年月"].Value = 0;

                CurrentUGR.Cells["对公实付金额"].Value = 0;
                CurrentUGR.Cells["对私实付金额"].Value = 0;
                CurrentUGR.Cells["应付实付金额"].Value = 0;

                
                CurrentUGR.Cells["经办人"].Value = "";
                CurrentUGR.Cells["对私收款人"].Value = "";
                CurrentUGR.Cells["是否直接开票"].Value = false;
                if (m_bIsPay)
                {
                    CurrentUGR.Cells["通关单费"].Value = 0;
                    CurrentUGR.Cells["报关费"].Value = 0;
                    CurrentUGR.Cells["港口建设费"].Value = 0;
                    CurrentUGR.Cells["GPS费"].Value = 0;
                    CurrentUGR.Cells["连柜费"].Value = 0;
                    CurrentUGR.Cells["加页费"].Value = 0;
                    CurrentUGR.Cells["查柜费"].Value = 0;
                    CurrentUGR.Cells["改船费"].Value = 0;
                    CurrentUGR.Cells["扫描费"].Value = 0;
                    CurrentUGR.Cells["茶水费"].Value = 0;
                    CurrentUGR.Cells["放行后查柜费"].Value = 0;
                    CurrentUGR.Cells["入仓代垫费"].Value = 0;
                    CurrentUGR.Cells["其他费用"].Value = 0;
                }
                else
                {
                    CurrentUGR.Cells["应收通关单费"].Value = 0;
                    CurrentUGR.Cells["应收报关费"].Value = 0;
                    CurrentUGR.Cells["应收港口建设费"].Value = 0;
                    CurrentUGR.Cells["应收GPS费"].Value = 0;
                    CurrentUGR.Cells["应收连柜费"].Value = 0;
                    CurrentUGR.Cells["应收加页费"].Value = 0;
                    CurrentUGR.Cells["应收查柜费"].Value = 0;
                    CurrentUGR.Cells["应收改船费"].Value = 0;
                    CurrentUGR.Cells["应收扫描费"].Value = 0;
                    CurrentUGR.Cells["应收茶水费"].Value = 0;
                    CurrentUGR.Cells["应收放行后查柜费"].Value = 0;
                    CurrentUGR.Cells["应收入仓代垫费"].Value = 0;
                    CurrentUGR.Cells["应收其他费用"].Value = 0;
                }

                CurrentUGR.Cells["备注"].Value = "";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                if (m_bClearing)
                {
                    m_bClearing = false;
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                int nFeeBGTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab
                    = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);


                string strContractNo = CurrentUGR.Cells["合同编号"].Value.ToString();

                if (CurrentUGR.Cells["实付日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["实付日期"].Value.ToString());
                    modelFeeBGTruePayTab.TruePayDate = dtTemp.Date;
                }

                if(modelFeeBGTruePayTab.ToPrivateBianHao == "" && modelFeeBGTruePayTab.ToPubBianHao == "")
                    modelFeeBGTruePayTab.InputDate = System.DateTime.Now.Date;

                modelFeeBGTruePayTab.TruePayMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
                modelFeeBGTruePayTab.ShouldPayMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
                //       modelFeeBGTruePayTab.ShouldRecvNotRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["实付金额"].Value.ToString()); ;

               // modelFeeBGTruePayTab.ToPubFormula = CurrentUGR.Cells["对公公式"].Value.ToString();
               // modelFeeBGTruePayTab.ToPrivateFormula = CurrentUGR.Cells["对私公式"].Value.ToString();
                //if (CurrentUGR.Cells["对公公式"].Value.ToString() != ""
                //    && CurrentUGR.Cells["对私公式"].Value.ToString() != "")
                //{
                //    MessageBox.Show("对公公式,对私公式不能同时添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return false;
                //}

                int nFiledReserv1 = modelFeeBGTruePayTab.intFiledReserv1;
                int nFiledReserv2 = modelFeeBGTruePayTab.intFiledReserv2;

                string strBGH = CurrentUGR.Cells["转关报关行"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelFeeBGTruePayTab.intFiledReserv1 = Convert.ToInt32(CurrentUGR.Cells["转关报关行"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBGHDataTab.GetList("BGHNameJX = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFeeBGTruePayTab.intFiledReserv1 = Convert.ToInt32(ds.Tables[0].Rows[0]["BGHDataTabID"].ToString(), 10);
                    }
                }

                string strHarbor = CurrentUGR.Cells["转关报关港"].Value.ToString();
                if (ClassInputValid.IsNum(strHarbor))
                {
                    modelFeeBGTruePayTab.intFiledReserv2 = Convert.ToInt32(CurrentUGR.Cells["转关报关港"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBGHarborTab.GetList("BGHarborName = '" + strHarbor + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFeeBGTruePayTab.intFiledReserv2 = Convert.ToInt32(ds.Tables[0].Rows[0]["BGHarborTabID"].ToString(), 10);
                    }
                }

                modelFeeBGTruePayTab.bitFiledReserv1 = (bool)CurrentUGR.Cells["是否直接开票"].Value;
                if (m_bIsPay)
                {
                    modelFeeBGTruePayTab.TGDFee = Convert.ToDecimal(CurrentUGR.Cells["通关单费"].Value.ToString());
                    modelFeeBGTruePayTab.BGFee = Convert.ToDecimal(CurrentUGR.Cells["报关费"].Value.ToString());
                    modelFeeBGTruePayTab.GKJSFee = Convert.ToDecimal(CurrentUGR.Cells["港口建设费"].Value.ToString());
                    modelFeeBGTruePayTab.KAFee = Convert.ToDecimal(CurrentUGR.Cells["GPS费"].Value.ToString());

                    modelFeeBGTruePayTab.LGFee = Convert.ToDecimal(CurrentUGR.Cells["连柜费"].Value.ToString());
                    modelFeeBGTruePayTab.JYFee = Convert.ToDecimal(CurrentUGR.Cells["加页费"].Value.ToString());
                    modelFeeBGTruePayTab.CGFee = Convert.ToDecimal(CurrentUGR.Cells["查柜费"].Value.ToString());
                    modelFeeBGTruePayTab.GCFee = Convert.ToDecimal(CurrentUGR.Cells["改船费"].Value.ToString());

                    modelFeeBGTruePayTab.SMFee = Convert.ToDecimal(CurrentUGR.Cells["扫描费"].Value.ToString());
                    modelFeeBGTruePayTab.CSFee = Convert.ToDecimal(CurrentUGR.Cells["茶水费"].Value.ToString());
                    modelFeeBGTruePayTab.OtherFee = Convert.ToDecimal(CurrentUGR.Cells["其他费用"].Value.ToString());


                    modelFeeBGTruePayTab.BGKAFee = Convert.ToDecimal(CurrentUGR.Cells["应收GPS费"].Value.ToString());
                    modelFeeBGTruePayTab.BGCGFee = Convert.ToDecimal(CurrentUGR.Cells["应收查柜费"].Value.ToString());
                    modelFeeBGTruePayTab.BGGCFee = Convert.ToDecimal(CurrentUGR.Cells["应收改船费"].Value.ToString());
                    modelFeeBGTruePayTab.BGSMFee = Convert.ToDecimal(CurrentUGR.Cells["应收扫描费"].Value.ToString());
                    modelFeeBGTruePayTab.BGCSFee = Convert.ToDecimal(CurrentUGR.Cells["应收茶水费"].Value.ToString());
                    modelFeeBGTruePayTab.BGOtherFee = Convert.ToDecimal(CurrentUGR.Cells["应收其他费用"].Value.ToString());

                    /****************************************************************************************/
                    modelFeeBGTruePayTab.FHCGFee = Convert.ToDecimal(CurrentUGR.Cells["放行后查柜费"].Value.ToString());   //放行后查柜费
                    modelFeeBGTruePayTab.RCDDFee = Convert.ToDecimal(CurrentUGR.Cells["入仓代垫费"].Value.ToString());   //入仓代垫费
                    /****************************************************************************************/
                }
                else
                {
                    modelFeeBGTruePayTab.BGTGDFee = Convert.ToDecimal(CurrentUGR.Cells["应收通关单费"].Value.ToString());
                    modelFeeBGTruePayTab.BGBGFee = Convert.ToDecimal(CurrentUGR.Cells["应收报关费"].Value.ToString());
                    modelFeeBGTruePayTab.BGGKJSFee = Convert.ToDecimal(CurrentUGR.Cells["应收港口建设费"].Value.ToString());
                    modelFeeBGTruePayTab.BGKAFee = Convert.ToDecimal(CurrentUGR.Cells["应收GPS费"].Value.ToString());

                    modelFeeBGTruePayTab.BGLGFee = Convert.ToDecimal(CurrentUGR.Cells["应收连柜费"].Value.ToString());
                    modelFeeBGTruePayTab.BGJYFee = Convert.ToDecimal(CurrentUGR.Cells["应收加页费"].Value.ToString());
                    modelFeeBGTruePayTab.BGCGFee = Convert.ToDecimal(CurrentUGR.Cells["应收查柜费"].Value.ToString());
                    modelFeeBGTruePayTab.BGGCFee = Convert.ToDecimal(CurrentUGR.Cells["应收改船费"].Value.ToString());

                    modelFeeBGTruePayTab.BGSMFee = Convert.ToDecimal(CurrentUGR.Cells["应收扫描费"].Value.ToString());
                    modelFeeBGTruePayTab.BGCSFee = Convert.ToDecimal(CurrentUGR.Cells["应收茶水费"].Value.ToString());
                    modelFeeBGTruePayTab.BGOtherFee = Convert.ToDecimal(CurrentUGR.Cells["应收其他费用"].Value.ToString());

                    /****************************************************************************************/
                    modelFeeBGTruePayTab.BGFHCGFee = Convert.ToDecimal(CurrentUGR.Cells["应收放行后查柜费"].Value.ToString());   //应收放行后查柜费
                    modelFeeBGTruePayTab.BGRCDDFee = Convert.ToDecimal(CurrentUGR.Cells["应收入仓代垫费"].Value.ToString());   //应收入仓代垫费
                    /****************************************************************************************/
                }

                modelFeeBGTruePayTab.isChaGui = Convert.ToBoolean(CurrentUGR.Cells["是否已核对金额"].Value.ToString());
                //modelFeeBGTruePayTab.isGaiChuan = Convert.ToBoolean(CurrentUGR.Cells["是否改船"].Value.ToString());
                //modelFeeBGTruePayTab.isSaoMiao = Convert.ToBoolean(CurrentUGR.Cells["是否扫描"].Value.ToString());

                modelFeeBGTruePayTab.JinBanPerson = modelFeeBGTruePayTab.JinBanPerson != ""
                    ? CurrentUGR.Cells["经办人"].Value.ToString()
                    : Frm_Main.m_strLoginUser;
                modelFeeBGTruePayTab.RecvMonyePerson = modelFeeBGTruePayTab.RecvMonyePerson != ""
                    ? CurrentUGR.Cells["对私收款人"].Value.ToString()
                    : this.comboBoxRecvPerson.Text;

                //modelFeeBGTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeBGTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeBGTruePayTab.Note = CurrentUGR.Cells["备注"].Value.ToString();


                //if (modelFeeBGTruePayTab.ToPubFormula != "")
                //    modelFeeBGTruePayTab.PubTruePayMoney
                //        = this.ultraCalcManager1.Calculate(modelFeeBGTruePayTab.ToPubFormula).ToDecimal();
                //if (modelFeeBGTruePayTab.ToPrivateFormula != "")
                //    modelFeeBGTruePayTab.PrivateTruePayMoney
                //        = this.ultraCalcManager1.Calculate(modelFeeBGTruePayTab.ToPrivateFormula).ToDecimal();

                modelFeeBGTruePayTab.PubTruePayMoney = (Decimal)CurrentUGR.Cells["对公实付金额"].Value;
                modelFeeBGTruePayTab.PrivateTruePayMoney = (Decimal)CurrentUGR.Cells["对私实付金额"].Value;
                //modelFeeBGTruePayTab.PubTruePayMoney = (Decimal)CurrentUGR.Cells["应付实付金额"].Value;
                
                //CurrentUGR.Cells["对公实付金额"].Value = modelFeeBGTruePayTab.PubTruePayMoney;
                //CurrentUGR.Cells["对私实付金额"].Value = modelFeeBGTruePayTab.PrivateTruePayMoney;
                //CurrentUGR.Cells["应付实付金额"].Value = modelFeeBGTruePayTab.PubTruePayMoney
                //    + modelFeeBGTruePayTab.PrivateTruePayMoney;
                CurrentUGR.Cells["对私收款人"].Value = modelFeeBGTruePayTab.RecvMonyePerson;
                CurrentUGR.Cells["经办人"].Value = modelFeeBGTruePayTab.JinBanPerson;
                CurrentUGR.Cells["录入日期"].Value = modelFeeBGTruePayTab.InputDate;

                if (m_bIsPay)
                {
                    CurrentUGR.Cells["通关单费"].Value = modelFeeBGTruePayTab.TGDFee;
                    CurrentUGR.Cells["报关费"].Value = modelFeeBGTruePayTab.BGFee;
                    CurrentUGR.Cells["港口建设费"].Value = modelFeeBGTruePayTab.GKJSFee;
                    CurrentUGR.Cells["GPS费"].Value = modelFeeBGTruePayTab.KAFee;
                    CurrentUGR.Cells["连柜费"].Value = modelFeeBGTruePayTab.LGFee;
                    CurrentUGR.Cells["加页费"].Value = modelFeeBGTruePayTab.JYFee;
                    CurrentUGR.Cells["查柜费"].Value = modelFeeBGTruePayTab.CGFee;
                    CurrentUGR.Cells["改船费"].Value = modelFeeBGTruePayTab.GCFee;
                    CurrentUGR.Cells["扫描费"].Value = modelFeeBGTruePayTab.SMFee;
                    CurrentUGR.Cells["茶水费"].Value = modelFeeBGTruePayTab.CSFee;
                    CurrentUGR.Cells["其他费用"].Value = modelFeeBGTruePayTab.OtherFee;
                    CurrentUGR.Cells["放行后查柜费"].Value = modelFeeBGTruePayTab.FHCGFee;
                    CurrentUGR.Cells["入仓代垫费"].Value = modelFeeBGTruePayTab.RCDDFee;
                }
                else
                {
                    CurrentUGR.Cells["应收通关单费"].Value = modelFeeBGTruePayTab.BGTGDFee;
                    CurrentUGR.Cells["应收报关费"].Value = modelFeeBGTruePayTab.BGBGFee;
                    CurrentUGR.Cells["应收港口建设费"].Value = modelFeeBGTruePayTab.BGGKJSFee;
                    CurrentUGR.Cells["应收GPS费"].Value = modelFeeBGTruePayTab.BGKAFee;
                    CurrentUGR.Cells["应收连柜费"].Value = modelFeeBGTruePayTab.BGLGFee;
                    CurrentUGR.Cells["应收加页费"].Value = modelFeeBGTruePayTab.BGJYFee;
                    CurrentUGR.Cells["应收查柜费"].Value = modelFeeBGTruePayTab.BGCGFee;
                    CurrentUGR.Cells["应收改船费"].Value = modelFeeBGTruePayTab.BGGCFee;
                    CurrentUGR.Cells["应收扫描费"].Value = modelFeeBGTruePayTab.BGSMFee;
                    CurrentUGR.Cells["应收茶水费"].Value = modelFeeBGTruePayTab.BGCSFee;
                    CurrentUGR.Cells["应收其他费用"].Value = modelFeeBGTruePayTab.BGOtherFee;
                    CurrentUGR.Cells["应收放行后查柜费"].Value = modelFeeBGTruePayTab.BGFHCGFee;
                    CurrentUGR.Cells["应收入仓代垫费"].Value = modelFeeBGTruePayTab.BGRCDDFee;
                }

                bRet = IsFeeTruePayLocked(CurrentUGR) && CheckFeeTruePayModify(modelFeeBGTruePayTab);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTab);

                /*更新报关行数据*/
                if (nFiledReserv1 != modelFeeBGTruePayTab.intFiledReserv1)
                {
                    DataSet ds = m_bllBGHDataTab.GetList("BGHDataTabID = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        CurrentUGR.Cells["报关行"].Value = ds.Tables[0].Rows[0]["报关行简称"].ToString();
                    }
                }

                /*更新报关港数据*/
                if (nFiledReserv2 != modelFeeBGTruePayTab.intFiledReserv2)
                {
                    DataSet ds = m_bllBGHarborTab.GetList("BGHarborTabID = '" + strHarbor + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        CurrentUGR.Cells["报关港"].Value = ds.Tables[0].Rows[0]["报关港"].ToString();
                    }
                }

                Frm_Main.Log2DB(this.Text, "修改", strContractNo);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private bool ModifyGoodsNoContractNoAndFac(int nRowIndex)
        {
            try
            {
                if (m_bClearing)
                {
                    m_bClearing = false;
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                int nFeeBGNoContractNoAndFacTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTab
                    = m_FeeBGNoContractNoAndFacTab.GetModel(nFeeBGNoContractNoAndFacTabID);

                if (CurrentUGR.Cells["实付日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["实付日期"].Value.ToString());
                    modelBGNoContractNoAndFacTab.TruePayDate = dtTemp.Date;
                }

                if (modelBGNoContractNoAndFacTab.ToPrivateBianHao == "" && modelBGNoContractNoAndFacTab.ToPubBianHao == "")
                    modelBGNoContractNoAndFacTab.InputDate = System.DateTime.Now.Date;

                modelBGNoContractNoAndFacTab.TruePayMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
                modelBGNoContractNoAndFacTab.ShouldPayMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);

                int nFiledReserv1 = modelBGNoContractNoAndFacTab.BGHNID;
                int nFiledReserv2 = modelBGNoContractNoAndFacTab.BGHarborID;

                string strBGH = CurrentUGR.Cells["转关报关行"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelBGNoContractNoAndFacTab.BGHNID = Convert.ToInt32(CurrentUGR.Cells["转关报关行"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBGHDataTab.GetList("BGHNameJX = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBGNoContractNoAndFacTab.BGHNID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGHDataTabID"].ToString(), 10);
                    }
                }

                string strHarbor = CurrentUGR.Cells["转关报关港"].Value.ToString();
                if (ClassInputValid.IsNum(strHarbor))
                {
                    modelBGNoContractNoAndFacTab.BGHarborID = Convert.ToInt32(CurrentUGR.Cells["转关报关港"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBGHarborTab.GetList("BGHarborName = '" + strHarbor + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBGNoContractNoAndFacTab.BGHarborID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGHarborTabID"].ToString(), 10);
                    }
                }

                string strCompany = CurrentUGR.Cells["经营单位"].Value.ToString();
                if (ClassInputValid.IsNum(strCompany))
                {
                    modelBGNoContractNoAndFacTab.BYCompanyTabID = Convert.ToInt32(CurrentUGR.Cells["经营单位"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBYCompanyTab.GetList("CompanyNameJX = '" + strCompany + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBGNoContractNoAndFacTab.BYCompanyTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BYCompanyTabID"].ToString(), 10);
                    }
                }

                string strDept = CurrentUGR.Cells["部门"].Value.ToString();
                if (ClassInputValid.IsNum(strDept))
                {
                    modelBGNoContractNoAndFacTab.BYDepartTabID = Convert.ToInt32(CurrentUGR.Cells["部门"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBYDepartTab.GetList("DepartName = '" + strDept + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBGNoContractNoAndFacTab.BYDepartTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BYDepartTabID"].ToString(), 10);
                    }
                }

                modelBGNoContractNoAndFacTab.bDirectlyInvoice = (bool)CurrentUGR.Cells["是否直接开票"].Value;

                if (m_bIsPay)
                {
                    modelBGNoContractNoAndFacTab.OtherFee = Convert.ToDecimal(CurrentUGR.Cells["其他费用"].Value.ToString());
                }
                else
                {
                    modelBGNoContractNoAndFacTab.BGOtherFee = Convert.ToDecimal(CurrentUGR.Cells["应收其他费用"].Value.ToString());
                }

                modelBGNoContractNoAndFacTab.bIsChaGui = Convert.ToBoolean(CurrentUGR.Cells["是否已核对金额"].Value.ToString());
                modelBGNoContractNoAndFacTab.JinBanPerson = modelBGNoContractNoAndFacTab.JinBanPerson != ""
                    ? CurrentUGR.Cells["经办人"].Value.ToString()
                    : Frm_Main.m_strLoginUser;
                modelBGNoContractNoAndFacTab.PriRecvMonyePerson = modelBGNoContractNoAndFacTab.PriRecvMonyePerson != ""
                    ? CurrentUGR.Cells["对私收款人"].Value.ToString()
                    : this.comboBoxRecvPerson.Text;

                modelBGNoContractNoAndFacTab.Note = CurrentUGR.Cells["备注"].Value.ToString();

                modelBGNoContractNoAndFacTab.PubTruePayMoney = (Decimal)CurrentUGR.Cells["对公实付金额"].Value;
                modelBGNoContractNoAndFacTab.PrivateTruePayMoney = (Decimal)CurrentUGR.Cells["对私实付金额"].Value;
                //modelBGNoContractNoAndFacTab.PubTruePayMoney = (Decimal)CurrentUGR.Cells["应付实付金额"].Value;

                CurrentUGR.Cells["对私收款人"].Value = modelBGNoContractNoAndFacTab.PriRecvMonyePerson;
                CurrentUGR.Cells["经办人"].Value = modelBGNoContractNoAndFacTab.JinBanPerson;
                CurrentUGR.Cells["录入日期"].Value = modelBGNoContractNoAndFacTab.InputDate;

                if (m_bIsPay)
                {
                    CurrentUGR.Cells["其他费用"].Value = modelBGNoContractNoAndFacTab.OtherFee;                 
                }
                else
                {
                    CurrentUGR.Cells["应收其他费用"].Value = modelBGNoContractNoAndFacTab.BGOtherFee;
                }

                bRet = IsFeeTruePayLocked(CurrentUGR) && CheckFeeTruePayModify(modelBGNoContractNoAndFacTab);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                m_FeeBGNoContractNoAndFacTab.Update(modelBGNoContractNoAndFacTab);

                /*更新报关行数据*/
                // if (nFiledReserv1 != modelBGNoContractNoAndFacTab.BGHNID)
                {
                    DataSet ds = m_bllBGHDataTab.GetList("BGHDataTabID = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        CurrentUGR.Cells["报关行"].Value = ds.Tables[0].Rows[0]["报关行简称"].ToString();
                    }

                    if (17 == modelBGNoContractNoAndFacTab.BGHNID)
                    {
                        CurrentUGR.Cells["对公实付金额"].Value = modelBGNoContractNoAndFacTab.OtherFee;
                        CurrentUGR.Cells["对公收款人"].Value = ds.Tables[0].Rows[0]["报关行简称"].ToString();
                        CurrentUGR.Cells["对私收款人"].Value = "";
                    }
                    else
                    {
                        CurrentUGR.Cells["对公实付金额"].Value = modelBGNoContractNoAndFacTab.PubTruePayMoney;
                        CurrentUGR.Cells["对公收款人"].Value = modelBGNoContractNoAndFacTab.PubRecvMonyePerson;
                        CurrentUGR.Cells["对私收款人"].Value = modelBGNoContractNoAndFacTab.PriRecvMonyePerson;
                    }               
                }

                CurrentUGR.Cells["应付实付金额"].Value = (Decimal)CurrentUGR.Cells["对公实付金额"].Value + (Decimal)CurrentUGR.Cells["对私实付金额"].Value;

                /*更新报关港数据*/
                if (nFiledReserv2 != modelBGNoContractNoAndFacTab.BGHarborID)
                {
                    DataSet ds = m_bllBGHarborTab.GetList("BGHarborTabID = '" + strHarbor + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        CurrentUGR.Cells["报关港"].Value = ds.Tables[0].Rows[0]["报关港"].ToString();
                    }
                }

                CurrentUGR.Cells["部门"].Value = CurrentUGR.Cells["部门"].Text;
                CurrentUGR.Cells["经营单位"].Value = CurrentUGR.Cells["经营单位"].Text;
                
                Frm_Main.Log2DB(this.Text, "修改无合同号工厂记录", modelBGNoContractNoAndFacTab.FeeBGNoContractNoAndFacTabID.ToString());
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;

                }
                else//修改
                {
                    if (CurrentUGR.Cells["合同编号"].Value.ToString() != "")
                        ModifyGoodsDetail(e.Row.Index);
                    else
                        ModifyGoodsNoContractNoAndFac(e.Row.Index);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }

        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab = new DGBY.Model.FeeBGTruePayTab();
                modelFeeBGTruePayTab = m_modelFeeBGTruePayTab;

                modelFeeBGTruePayTab.ToPrivateBianHao = "";
                modelFeeBGTruePayTab.ToPubBianHao = "";
                modelFeeBGTruePayTab.TruePayDate = null;

                m_bllFeeBGTruePayTab.Add(modelFeeBGTruePayTab);

                DGBY.Model.PaidanRegisterTab modelPaidanRegisterTab = m_bllPaidanRegisterTab.GetModel(modelFeeBGTruePayTab.PaidanRegisterTabID);
                if (modelPaidanRegisterTab != null)
                    Frm_Main.Log2DB(this.Text, "添加", modelPaidanRegisterTab.ContractCode);
                else
                    Frm_Main.Log2DB(this.Text, "添加", modelFeeBGTruePayTab.FeeBGTruePayTabID.ToString());
                

                ShowAllRecord("FeeBGTruePayTab.PaidanRegisterTabID = '" + modelFeeBGTruePayTab.PaidanRegisterTabID.ToString() + "'", "FeeBGNoContractNoAndFacTab.PaidanRegisterTabID <> -1", m_bIsPay);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void InitNewAddRowValueCopyContract(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["合同编号"].Value = m_modelFeeBGTruePayTab.PaidanRegisterTabID;
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeBGTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeBGTruePayTab.ToPrivateBianHao;
            if (m_modelFeeBGTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeBGTruePayTab.TruePayDate;
            if (m_modelFeeBGTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeBGTruePayTab.InputDate;


            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeBGTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeBGTruePayTab.ShouldPayMonth;
            CurrentUGR.Cells["对公实付金额"].Value = m_modelFeeBGTruePayTab.PubTruePayMoney;
            CurrentUGR.Cells["对私实付金额"].Value = m_modelFeeBGTruePayTab.PrivateTruePayMoney;
            CurrentUGR.Cells["应付实付金额"].Value = m_modelFeeBGTruePayTab.PubTruePayMoney 
                + m_modelFeeBGTruePayTab.PrivateTruePayMoney;

           // CurrentUGR.Cells["对公公式"].Value = m_modelFeeBGTruePayTab.ToPubFormula;
           // CurrentUGR.Cells["对私公式"].Value = m_modelFeeBGTruePayTab.ToPrivateFormula;
            CurrentUGR.Cells["经办人"].Value = m_modelFeeBGTruePayTab.JinBanPerson;
            CurrentUGR.Cells["对私收款人"].Value = m_modelFeeBGTruePayTab.RecvMonyePerson;

           // CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeBGTruePayTab.IsToRecvPayTab;
           // CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeBGTruePayTab.IsToFinCheckMoneyTab;


            CurrentUGR.Cells["备注"].Value = m_modelFeeBGTruePayTab.Note;


            //CurrentUGR.Cells["通关单费"].Value = m_modelFeeBGTruePayTab.TGDFee;
            //CurrentUGR.Cells["报关费"].Value = m_modelFeeBGTruePayTab.BGFee;
            //CurrentUGR.Cells["港口建设费"].Value = m_modelFeeBGTruePayTab.GKJSFee;
            //CurrentUGR.Cells["GPS费"].Value = m_modelFeeBGTruePayTab.KAFee;
            //CurrentUGR.Cells["连柜费"].Value = m_modelFeeBGTruePayTab.LGFee;
            //CurrentUGR.Cells["加页费"].Value = m_modelFeeBGTruePayTab.JYFee;
            //CurrentUGR.Cells["查柜费"].Value = m_modelFeeBGTruePayTab.CGFee;
            //CurrentUGR.Cells["改船费"].Value = m_modelFeeBGTruePayTab.GCFee;
            //CurrentUGR.Cells["扫描费"].Value = m_modelFeeBGTruePayTab.SMFee;
            //CurrentUGR.Cells["茶水费"].Value = m_modelFeeBGTruePayTab.CSFee;
            //CurrentUGR.Cells["其他费用"].Value = m_modelFeeBGTruePayTab.OtherFee;
            if (m_bIsPay)
            {
                CurrentUGR.Cells["通关单费"].Value = m_modelFeeBGTruePayTab.TGDFee;
                CurrentUGR.Cells["报关费"].Value = m_modelFeeBGTruePayTab.BGFee;
                CurrentUGR.Cells["港口建设费"].Value = m_modelFeeBGTruePayTab.GKJSFee;
                CurrentUGR.Cells["GPS费"].Value = m_modelFeeBGTruePayTab.KAFee;
                CurrentUGR.Cells["连柜费"].Value = m_modelFeeBGTruePayTab.LGFee;
                CurrentUGR.Cells["加页费"].Value = m_modelFeeBGTruePayTab.JYFee;
                CurrentUGR.Cells["查柜费"].Value = m_modelFeeBGTruePayTab.CGFee;
                CurrentUGR.Cells["改船费"].Value = m_modelFeeBGTruePayTab.GCFee;
                CurrentUGR.Cells["扫描费"].Value = m_modelFeeBGTruePayTab.SMFee;
                CurrentUGR.Cells["茶水费"].Value = m_modelFeeBGTruePayTab.CSFee;
                CurrentUGR.Cells["放行后查柜费"].Value = m_modelFeeBGTruePayTab.FHCGFee;
                CurrentUGR.Cells["入仓代垫费"].Value = m_modelFeeBGTruePayTab.RCDDFee;
                CurrentUGR.Cells["其他费用"].Value = m_modelFeeBGTruePayTab.OtherFee;
            }
            else
            {
                CurrentUGR.Cells["应收通关单费"].Value = m_modelFeeBGTruePayTab.BGTGDFee;
                CurrentUGR.Cells["应收报关费"].Value = m_modelFeeBGTruePayTab.BGBGFee;
                CurrentUGR.Cells["应收港口建设费"].Value = m_modelFeeBGTruePayTab.BGGKJSFee;
                CurrentUGR.Cells["应收GPS费"].Value = m_modelFeeBGTruePayTab.BGKAFee;
                CurrentUGR.Cells["应收连柜费"].Value = m_modelFeeBGTruePayTab.BGLGFee;
                CurrentUGR.Cells["应收加页费"].Value = m_modelFeeBGTruePayTab.BGJYFee;
                CurrentUGR.Cells["应收查柜费"].Value = m_modelFeeBGTruePayTab.BGCGFee;
                CurrentUGR.Cells["应收改船费"].Value = m_modelFeeBGTruePayTab.BGGCFee;
                CurrentUGR.Cells["应收扫描费"].Value = m_modelFeeBGTruePayTab.BGSMFee;
                CurrentUGR.Cells["应收茶水费"].Value = m_modelFeeBGTruePayTab.BGCSFee;
                CurrentUGR.Cells["应收放行后查柜费"].Value = m_modelFeeBGTruePayTab.BGFHCGFee;
                CurrentUGR.Cells["应收入仓代垫费"].Value = m_modelFeeBGTruePayTab.BGRCDDFee;
                CurrentUGR.Cells["应收其他费用"].Value = m_modelFeeBGTruePayTab.BGOtherFee;
            }
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }

        private void InitNewAddRowValueNoContractAndFac(int nIndex)
        {
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetailNoContractNoAndFac(nIndex);
            }
        }

        private bool AddGoodsDetailNoContractNoAndFac(int nRowIndex)
        {
            try
            {
                DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTab = new DGBY.Model.FeeBGNoContractNoAndFacTab();

                modelBGNoContractNoAndFacTab.PaidanRegisterTabID = -1;
                modelBGNoContractNoAndFacTab.bNoManualGeneration = false;  
                modelBGNoContractNoAndFacTab.TruePayDate = null;

                modelBGNoContractNoAndFacTab.ToPrivateBianHao = "";
                modelBGNoContractNoAndFacTab.ToPubBianHao = "";

                modelBGNoContractNoAndFacTab.bDirectlyInvoice = false;
                modelBGNoContractNoAndFacTab.InputDate = System.DateTime.Now.Date;
                modelBGNoContractNoAndFacTab.SBDate = null;

                modelBGNoContractNoAndFacTab.ShouldPayTruePayFee = 0;
                modelBGNoContractNoAndFacTab.PubTruePayMoney = 0;
                modelBGNoContractNoAndFacTab.PrivateTruePayMoney = 0;
                
                modelBGNoContractNoAndFacTab.bIsChaGui = false;
                modelBGNoContractNoAndFacTab.OtherFee = 0;
                modelBGNoContractNoAndFacTab.BGOtherFee = 0;
                modelBGNoContractNoAndFacTab.ShouldReceiveFee = 0;

                modelBGNoContractNoAndFacTab.BGHNID = 33;
                modelBGNoContractNoAndFacTab.BGHarborID = 10;

                modelBGNoContractNoAndFacTab.JinBanPerson = "";
                modelBGNoContractNoAndFacTab.BYCompanyTabID = 3;
                modelBGNoContractNoAndFacTab.BYDepartTabID = 7;
       
                modelBGNoContractNoAndFacTab.PubRecvMonyePerson = "";
                modelBGNoContractNoAndFacTab.PriRecvMonyePerson = "";

                modelBGNoContractNoAndFacTab.ShouldPayMonth = 0;
                modelBGNoContractNoAndFacTab.TruePayMonth = 0;

                modelBGNoContractNoAndFacTab.Note = "";

                int nID = m_FeeBGNoContractNoAndFacTab.Add(modelBGNoContractNoAndFacTab);
                Frm_Main.Log2DB(this.Text, "增加无合同号工厂记录", nID.ToString());
                ShowAllRecord("FeeBGTruePayTab.FeeBGTruePayTabID = -1", "FeeBGNoContractNoAndFacTab.FeeBGNoContractNoAndFacTabID = '" + nID.ToString() + "'", true);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void 复制增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];

                    if (null == CurrentUGRFrom)
                        return;

                    if (CurrentUGRFrom.Cells["合同编号"].Value.ToString() == "")
                    {
                        MessageBox.Show("该类型记录不能复制增加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                        
                    int nSelectFeeBGTruePayTabID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    m_modelFeeBGTruePayTab = m_bllFeeBGTruePayTab.GetModel(nSelectFeeBGTruePayTabID);
                    m_modelFeeBGTruePayTab.InputDate = System.DateTime.Now.Date;
                    // m_bCopyInsertFlag = true;
                    m_InsertType = InsertType.Common;
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;

            if (m_InsertType == InsertType.Common)
                InitNewAddRowValueCopyContract(e.Row.Index);
            else if (m_InsertType == InsertType.WithoutContractNoAndFac)
                InitNewAddRowValueNoContractAndFac(e.Row.Index);
        }
        private bool CheckModify(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeLockTab.GetList("FeeName = '报关' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
        private bool IsFeeTruePayLocked(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeTruePayLockTab.GetList("FeeName = '报关' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        private bool CheckFeeTruePayModify(DGBY.Model.FeeBGNoContractNoAndFacTab modelFeeBGNoAndFacTab)
        {
            DGBY.Model.FeeBGNoContractNoAndFacTab oldmodelFeeBGNoAndFacTab = m_FeeBGNoContractNoAndFacTab.GetModel(modelFeeBGNoAndFacTab.FeeBGNoContractNoAndFacTabID);
            if (null != oldmodelFeeBGNoAndFacTab)
            {
                //限制修改实付日期、费用编号、是否直接开票、应付实付金额
                if (oldmodelFeeBGNoAndFacTab.TruePayDate != modelFeeBGNoAndFacTab.TruePayDate
                    || oldmodelFeeBGNoAndFacTab.bDirectlyInvoice != modelFeeBGNoAndFacTab.bDirectlyInvoice
                    || oldmodelFeeBGNoAndFacTab.OtherFee != modelFeeBGNoAndFacTab.OtherFee)
                    return true;
            }

            return false;
        }
        private bool CheckFeeTruePayModify(DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab)
        {
            DGBY.Model.FeeBGTruePayTab oldModelFeeBGTruePayTab = m_bllFeeBGTruePayTab.GetModel(modelFeeBGTruePayTab.FeeBGTruePayTabID);
            if (null != oldModelFeeBGTruePayTab)
            {
                //限制修改实付日期、费用编号、是否直接开票、应付实付金额
                if (oldModelFeeBGTruePayTab.TruePayDate != modelFeeBGTruePayTab.TruePayDate
                    || oldModelFeeBGTruePayTab.bitFiledReserv1 != modelFeeBGTruePayTab.bitFiledReserv1
                    || oldModelFeeBGTruePayTab.TGDFee != modelFeeBGTruePayTab.TGDFee
                    || oldModelFeeBGTruePayTab.BGFee != modelFeeBGTruePayTab.BGFee
                    || oldModelFeeBGTruePayTab.GKJSFee != modelFeeBGTruePayTab.GKJSFee
                    || oldModelFeeBGTruePayTab.KAFee != modelFeeBGTruePayTab.KAFee
                    || oldModelFeeBGTruePayTab.CGFee != modelFeeBGTruePayTab.CGFee
                    || oldModelFeeBGTruePayTab.LGFee != modelFeeBGTruePayTab.LGFee
                    || oldModelFeeBGTruePayTab.JYFee != modelFeeBGTruePayTab.JYFee
                    || oldModelFeeBGTruePayTab.GCFee != modelFeeBGTruePayTab.GCFee
                    || oldModelFeeBGTruePayTab.SMFee != modelFeeBGTruePayTab.SMFee
                    || oldModelFeeBGTruePayTab.CSFee != modelFeeBGTruePayTab.CSFee
                    || oldModelFeeBGTruePayTab.OtherFee != modelFeeBGTruePayTab.OtherFee)
                    return true;
            }

            return false;
        }
        private void 删除本行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                    bool bRet = CheckModify(CurrentUGR);
                    if (!bRet)
                    {
                        MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    bRet = IsFeeTruePayLocked(CurrentUGR);
                    if (bRet)
                    {
                        MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    if (CurrentUGR.Cells["合同编号"].Value.ToString() != "")
                    {
                        int nPaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["PaidanID"].Value.ToString(), 10);
                        DataSet ds = m_bllFeeBGTruePayTab.GetList("FeeBGTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'", "");
                        if (ds.Tables[0].Rows.Count > 1)
                        {
                            //int nFeeBGTruePayTabID = Convert.ToInt32(CurrentUGR.Cells["FeeBGTruePayTabID"].Value.ToString(), 10);


                            //ds = m_bllFeeBGTruePayTab.GetList("FeeBGTruePayTab.FeeBGTruePayTabID = '" + nFeeBGTruePayTabID.ToString() + "'");
                            ultraGrid1.ActiveRow.Delete(false);
                        }
                        else
                        {
                            MessageBox.Show("系统中该合同号只剩下一条记录，为系统自动生成，不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    else
                    {
                        ultraGrid1.ActiveRow.Delete(false);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                if (ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString() != "")
                {
                    int nSelectFeeBGTruePayTabID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells["FeeID"].Value.ToString(), 10);
                    DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                        = m_bllFeeBGTruePayTab.GetModel(nSelectFeeBGTruePayTabID);

                    if (modelFeeBGTruePayTabOld == null)
                    {
                        MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        try
                        {
                            m_bllFeeBGTruePayTab.Delete(nSelectFeeBGTruePayTabID);
                            Frm_Main.Log2DB(this.Text, "删除", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            e.Cancel = true;
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
                else
                {
                    int nSelectFeeID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells["FeeID"].Value.ToString(), 10);
                    DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTab
                        = m_FeeBGNoContractNoAndFacTab.GetModel(nSelectFeeID);

                    if (modelBGNoContractNoAndFacTab == null)
                    {
                        MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        try
                        {
                            m_FeeBGNoContractNoAndFacTab.Delete(nSelectFeeID);
                            Frm_Main.Log2DB(this.Text, "删除无合同号工厂记录", modelBGNoContractNoAndFacTab.FeeBGNoContractNoAndFacTabID.ToString());
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            e.Cancel = true;
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void SyncSBMonth()
        {
            return;
            DataSet dsTemp = m_bllHxdNoTab.ShowZhDjbRec("year(HxdNoTab.SBDate) > 1900");
            DataSet ds = m_bllFeeBGTruePayTab.GetList("ShouldPayMonth = 0 or TruePayMonth = 0", "");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                string strContractNo = ds.Tables[0].Rows[j]["合同编号"].ToString();
                int nFeeBGTruePayTabID = (int)ds.Tables[0].Rows[j]["FeeBGTruePayTabID"];
                DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
               = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

                DataRow[] Rows = dsTemp.Tables[0].Select("合同编号 = '" + strContractNo.ToString() + "'");
                if (Rows.Length > 0)
                {
                    DateTime dtSBMonth = (DateTime)Rows[0]["申报日期"];
                    int nSBMonth = dtSBMonth.Month;
                    modelFeeBGTruePayTabOld.ShouldPayMonth = nSBMonth;
                    modelFeeBGTruePayTabOld.TruePayMonth = nSBMonth;
                    m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTabOld);
                }

            }
            //ShowAllRecord("");
            //  MessageBox.Show("同步申报月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private string MakeBGBianHao(string strDepart, string strComp
        , string strPuborPri, string strDate)
        {

            string strRet = "BG" + ClassUtility.m_dicCn2EngDep[strDepart]
                + ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;

            if (m_strBianHaoTemp != "")
            {
                strRet += m_strBianHaoTemp;
            }
            else
            {
                DateTime dtTruePay = this.dateTimePicker1.Value.Date;
                DataSet dsTemp;
                //if (strPuborPri == "公")
                //    dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + @"' and ToPubBianHao like '%" + strRet + "%'");
                //else

                dsTemp = m_bllFeeBGTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + "'");


                if (dsTemp.Tables[0].Rows.Count > 0)
                {
                    string strTest = "";

                    string strTestPub = dsTemp.Tables[0].Rows[0]["对公费用编号"].ToString();

                    string strTestPri = dsTemp.Tables[0].Rows[0]["对私费用编号"].ToString();

                    if (strTestPri.CompareTo(strTestPub) >= 0)
                        strTest = strTestPri;
                    else
                        strTest = strTestPub;


                    string strTemp = strTest.Remove(0, 8);
                    if (strTemp != "")
                    {
                        int nTemp = Convert.ToInt32(strTemp, 10) + 1;
                        strTemp = nTemp.ToString().PadLeft(3, '0'); ;
                    }
                    else
                    {
                        strTemp = "001";
                    }

                    strRet += strTemp;
                    m_strBianHaoTemp = strTemp;
                }
                else
                {
                    strRet = strRet + "001";
                    m_strBianHaoTemp = "001";
                }
            }
            return strRet;
        }
        private string MakeBGBianHaoWithoutLiuShuiNo(string strDepart, string strComp
          , string strPuborPri, string strDate)
        {
            string strRet = "BG" + ClassUtility.m_dicCn2EngDep[strDepart]
+ ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;

           // string strRet = "BG" + strDepart + strComp + strPuborPri + strDate;
            return strRet;
        }
        private void buttonBatchModifyDate_Click(object sender, EventArgs e)
        {
            m_strBianHaoTemp = "";
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;
                for (int i = 0; i < ugr.Length; i++)
                {
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);

                    if (ugr[i].Cells["合同编号"].Value.ToString() != "")
                    {
                        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                                        = m_bllFeeBGTruePayTab.GetModel(nID);

                        DateTime? dtTruePayOld = modelFeeBGTruePayTabOld.TruePayDate;
                        modelFeeBGTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;
                        
                        UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                        if (dtTruePayOld == null
                            && (((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0) || ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0)))
                        {
                            if ((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHao(ugr[i].Cells["部门"].Value.ToString()
                               , ugr[i].Cells["经营单位"].Value.ToString()
                               , "私"
                                , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect);

                                modelFeeBGTruePayTabOld.ToPrivateBianHao = strRet;
                                CurrentUGR.Cells["对私费用编号"].Value = strRet;
                            }
                            if ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHao(ugr[i].Cells["部门"].Value.ToString()
                               , ugr[i].Cells["经营单位"].Value.ToString()
                               , "公"
                                , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect);

                                modelFeeBGTruePayTabOld.ToPubBianHao = strRet;
                                CurrentUGR.Cells["对公费用编号"].Value = strRet;
                            }

                            modelFeeBGTruePayTabOld.bitFiledReserv2 = false;//编号是否手动生成

                            CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                            CurrentUGR.Cells["编号是否手动生成"].Value = false;

                            m_bllFeeBGTruePayTab.TransUpdate(modelFeeBGTruePayTabOld
                                , m_SQLStringList, m_arrayList, i);
                        }
                        //CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                    }
                    else
                    {
                        DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTabOld
                           = m_FeeBGNoContractNoAndFacTab.GetModel(nID);

                        DateTime? dtTruePayOld = modelBGNoContractNoAndFacTabOld.TruePayDate;
                        modelBGNoContractNoAndFacTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                        UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                        if (dtTruePayOld == null
                            && (((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0) || ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0)))
                        {
                            string strDept = ugr[i].Cells["部门"].Text;
                            string strComp = ugr[i].Cells["经营单位"].Text;

                            if (String.IsNullOrEmpty(strDept))
                            {
                                MessageBox.Show("部门未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }

                            if (String.IsNullOrEmpty(strComp))
                            {
                                MessageBox.Show("经营单位未设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }


                            if ((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHao(ugr[i].Cells["部门"].Value.ToString()
                               , ugr[i].Cells["经营单位"].Value.ToString()
                               , "私"
                                , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect);

                                modelBGNoContractNoAndFacTabOld.ToPrivateBianHao = strRet;
                                CurrentUGR.Cells["对私费用编号"].Value = strRet;
                            }
                            if ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHao(ugr[i].Cells["部门"].Value.ToString()
                               , ugr[i].Cells["经营单位"].Value.ToString()
                               , "公"
                                , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect);

                                modelBGNoContractNoAndFacTabOld.ToPubBianHao = strRet;
                                CurrentUGR.Cells["对公费用编号"].Value = strRet;
                            }

                            modelBGNoContractNoAndFacTabOld.bNoManualGeneration = false;//编号是否手动生成

                            CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                            CurrentUGR.Cells["编号是否手动生成"].Value = false;

                            m_FeeBGNoContractNoAndFacTab.TransUpdate(modelBGNoContractNoAndFacTabOld
                                , m_SQLStringList, m_arrayList, i);
                        }
                        else
                        {
                            if (null != dtTruePayOld)
                            {
                                MessageBox.Show("实付日期已设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }
                            else if(((Decimal)CurrentUGR.Cells["对私实付金额"].Value == 0) && ((Decimal)CurrentUGR.Cells["对公实付金额"].Value == 0))
                            {
                                MessageBox.Show("对私实付金额或对公实付金额须设置其一!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }
                        }
                    }
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改实付日期", this.dateTimePicker1.Value.Date.ToString());
                MessageBox.Show("批量修改实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void ultraGrid1_MouseDown(object sender, MouseEventArgs e)
        {
            UltraGrid grid = (UltraGrid)sender;
            UIElement element = grid.DisplayLayout.UIElement.LastElementEntered;
            if (element == null)
                return;
            Infragistics.Win.UltraWinGrid.ColumnHeader columnHeader = element.GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader)) as Infragistics.Win.UltraWinGrid.ColumnHeader;
            if (columnHeader != null)
            {
                string str = columnHeader.Caption;
                // string strtype = grid.Rows[0].Cells[str].GetType().ToString();
                if (str != "应付实付金额")
                    return;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                if (ugr.Length <= 0)
                {
                    return;
                }
                Decimal dtTemp = 0;
                for (int i = 0; i < ugr.Length; i++)
                {
                    string strTemp = ugr[i].Cells[str].Value.ToString();
                    if (strTemp != "")
                        dtTemp += Convert.ToDecimal(ugr[i].Cells[str].Value.ToString());
                }
                this.textBoxTotal.Text = dtTemp.ToString();
            }
            else
            {

            }
        }

        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonSetEmpty_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                bool bCanModify = true;

                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

             
                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                List<string> lstDT = new List<string>();
                for (int i = 0; i < ugr.Length; i++)
                {
                    string strTemp = ugr[i].Cells["对公费用编号"].Value.ToString() + ugr[i].Cells["对私费用编号"].Value.ToString();

                    if (strTemp != "" && lstDT.Count == 0)
                        lstDT.Add(strTemp);
                    else if (lstDT.Count > 0 && !lstDT.Contains(strTemp))
                    {
                        bCanModify = false;
                        break;
                    }

                }

                if (!bCanModify)
                {
                    MessageBox.Show("费用编号不相同，不能批量置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);

                    if (ugr[i].Cells["合同编号"].Value.ToString() != "")
                    {
                        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                                        = m_bllFeeBGTruePayTab.GetModel(nID);
                        modelFeeBGTruePayTabOld.TruePayDate = null;
                        modelFeeBGTruePayTabOld.ToPrivateBianHao = "";
                        modelFeeBGTruePayTabOld.ToPubBianHao = "";

                        m_bllFeeBGTruePayTab.TransUpdate(modelFeeBGTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);
                    }
                    else
                    {
                        DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTabOld
                            = m_FeeBGNoContractNoAndFacTab.GetModel(nID);
                        modelBGNoContractNoAndFacTabOld.TruePayDate = null;
                        modelBGNoContractNoAndFacTabOld.ToPrivateBianHao = "";
                        modelBGNoContractNoAndFacTabOld.ToPubBianHao = "";

                        m_FeeBGNoContractNoAndFacTab.TransUpdate(modelBGNoContractNoAndFacTabOld
                             , m_SQLStringList, m_arrayList, i);
                    }

                    CurrentUGR.Cells["对私费用编号"].Value = "";
                    CurrentUGR.Cells["对公费用编号"].Value = "";
                    CurrentUGR.Cells["实付日期"].Value = DBNull.Value;
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空实付日期", "");
                MessageBox.Show("批量置空实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void comboBoxBGH_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxBGH.SelectedValue.ToString();
                int nID = Convert.ToInt32(str, 10);
                DataSet ds = m_bllBGHRecvPeoTelTab.GetList("BGHDataTabID = '" + nID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.comboBoxRecvPerson.DataSource = ds.Tables[0];

                    this.comboBoxRecvPerson.ValueMember = "BGHRecvPeoTelTabID";
                    this.comboBoxRecvPerson.DisplayMember = "报关行收款人";
                    this.comboBoxRecvPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
                    this.comboBoxRecvPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    this.comboBoxRecvPerson.SelectedIndex = 0;
                }
                else
                {
                    this.comboBoxRecvPerson.DataSource = null;
                }
   
            

            }
            catch { }
        }

        private void buttonShowAllRecv_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView(false);
        }
        //private void UpdateChaGui(CellEventArgs e)
        //{
        //    int nIndex = e.Cell.Row.Index;
        //    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

        //    bool bIsChaGui = (bool)e.Cell.Value;
        //    if (bIsChaGui)
        //    {
        //        int nFeeBGTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
        //        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab
        //            = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

        //        DataSet dsInfo = m_bllBGCKHTTab.ShowContractInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + modelFeeBGTruePayTab.PaidanRegisterTabID.ToString() + "'");

        //        if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
        //        {
        //            modelFeeBGTruePayTab.CGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付查柜费定价"];
        //            modelFeeBGTruePayTab.BGCGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收查柜费定价"];
        //            m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTab);
        //        }
        //        if (m_bIsPay)
        //        {
        //            CurrentUGR.Cells["查柜费"].Value = modelFeeBGTruePayTab.CGFee;
        //        }
        //        else
        //        {
        //            CurrentUGR.Cells["应收查柜费"].Value = modelFeeBGTruePayTab.BGCGFee;
        //        }
        //    }
        //    else
        //    {
        //        if (m_bIsPay)
        //        {
        //            CurrentUGR.Cells["查柜费"].Value = 0;
        //        }
        //        else
        //        {
        //            CurrentUGR.Cells["应收查柜费"].Value = 0;
        //        }
        //    }
        //}
        //private void UpdateGaiChuan(CellEventArgs e)
        //{
        //    int nIndex = e.Cell.Row.Index;
        //    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

        //    bool bIsGaiChuan = (bool)e.Cell.Value;
        //    if (bIsGaiChuan)
        //    {
        //        int nFeeBGTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
        //        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab
        //            = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

        //        DataSet dsInfo = m_bllBGCKHTTab.ShowContractInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + modelFeeBGTruePayTab.PaidanRegisterTabID.ToString() + "'");

        //        if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
        //        {
        //            modelFeeBGTruePayTab.GCFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付改船费定价"];
        //            modelFeeBGTruePayTab.BGGCFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收改船费定价"];
        //            m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTab);
        //        }
        //        if (m_bIsPay)
        //        {
        //            CurrentUGR.Cells["改船费"].Value = modelFeeBGTruePayTab.GCFee;
        //        }
        //        else
        //        {
        //            CurrentUGR.Cells["应收改船费"].Value = modelFeeBGTruePayTab.BGGCFee;
        //        }
        //    }
        //    else
        //    {
        //        if (m_bIsPay)
        //        {
        //            CurrentUGR.Cells["改船费"].Value = 0;
        //        }
        //        else
        //        {
        //            CurrentUGR.Cells["应收改船费"].Value = 0;
        //        }
        //    }
        //}
        //private void UpdateSM(CellEventArgs e)
        //{
        //    int nIndex = e.Cell.Row.Index;
        //    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

        //    bool bIsSaoMiao = (bool)e.Cell.Value;
        //    if (bIsSaoMiao)
        //    {
        //        int nFeeBGTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
        //        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTab
        //            = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

        //        DataSet dsInfo = m_bllBGCKHTTab.ShowContractInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + modelFeeBGTruePayTab.PaidanRegisterTabID.ToString() + "'");

        //        if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
        //        {
        //            modelFeeBGTruePayTab.SMFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付扫描费定价"];
        //            modelFeeBGTruePayTab.BGSMFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收扫描费定价"];
        //            m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTab);
        //        }
        //        if (m_bIsPay)
        //        {
        //            CurrentUGR.Cells["扫描费"].Value = modelFeeBGTruePayTab.SMFee;
        //        }
        //        else
        //        {
        //            CurrentUGR.Cells["应收扫描费"].Value = modelFeeBGTruePayTab.BGSMFee;
        //        }
        //    }
        //    else
        //    {
        //        if (m_bIsPay)
        //        {
        //            CurrentUGR.Cells["扫描费"].Value = 0;
        //        }
        //        else
        //        {
        //            CurrentUGR.Cells["应收扫描费"].Value = 0;
        //        }
        //    }
        //}

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
            try
            {
                if (m_bIsPay)
                {
                    int nIndex = e.Cell.Row.Index;
                    if (nIndex < 0)
                        return;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

                    if (CurrentUGR.Cells["合同编号"].Value.ToString() == "")
                        return;

                    if (e.Cell.Column.Key == "通关单费" || e.Cell.Column.Key == "港口建设费"
                         || e.Cell.Column.Key == "GPS费" || e.Cell.Column.Key == "连柜费"
                         || e.Cell.Column.Key == "加页费" || e.Cell.Column.Key == "查柜费"
                         || e.Cell.Column.Key == "改船费" || e.Cell.Column.Key == "扫描费"
                         || e.Cell.Column.Key == "茶水费" || e.Cell.Column.Key == "放行后查柜费"
                         || e.Cell.Column.Key == "入仓代垫费" || e.Cell.Column.Key == "其他费用")
                    {



                        CurrentUGR.Cells["对私实付金额"].Value
                            = (Decimal)CurrentUGR.Cells["通关单费"].Value
                            + (Decimal)CurrentUGR.Cells["港口建设费"].Value
                            + (Decimal)CurrentUGR.Cells["GPS费"].Value
                            + (Decimal)CurrentUGR.Cells["连柜费"].Value
                            + (Decimal)CurrentUGR.Cells["加页费"].Value
                            + (Decimal)CurrentUGR.Cells["查柜费"].Value
                            + (Decimal)CurrentUGR.Cells["改船费"].Value
                            + (Decimal)CurrentUGR.Cells["扫描费"].Value
                            + (Decimal)CurrentUGR.Cells["茶水费"].Value
                            + (Decimal)CurrentUGR.Cells["放行后查柜费"].Value
                            + (Decimal)CurrentUGR.Cells["入仓代垫费"].Value
                            + (Decimal)CurrentUGR.Cells["其他费用"].Value;

                        CurrentUGR.Cells["应付实付金额"].Value
                      = (Decimal)CurrentUGR.Cells["对公实付金额"].Value
                      + (Decimal)CurrentUGR.Cells["对私实付金额"].Value;

                    }
                    else if (e.Cell.Column.Key == "报关费")
                    {
                        CurrentUGR.Cells["对公实付金额"].Value = CurrentUGR.Cells["报关费"].Value;
                        CurrentUGR.Cells["应付实付金额"].Value
                      = (Decimal)CurrentUGR.Cells["对公实付金额"].Value
                      + (Decimal)CurrentUGR.Cells["对私实付金额"].Value;
                    }

                  


                }
            }
            catch { }
           
         
        
        }

        private void buttonModYearMon_Click(object sender, EventArgs e)
        {
            if (!ClassInputValid.IsNum(textBoxYearMonth.Text))
            {
                errorProvider1.SetError(textBoxYearMonth, "必须输入数字");
                return;
            }
            int nYearMon = Convert.ToInt32(textBoxYearMonth.Text, 10);
            if (nYearMon < 201001 || nYearMon > 219912)
            {
                errorProvider1.SetError(textBoxYearMonth, "必须正确的年月，形如201206");
                return;
            }

            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                    if (ugr[i].Cells["合同编号"].Value.ToString() != "")
                    {
                        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                                            = m_bllFeeBGTruePayTab.GetModel(nID);
                        modelFeeBGTruePayTabOld.ShouldPayMonth = nYearMon;


                        m_bllFeeBGTruePayTab.TransUpdate(modelFeeBGTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);
                    }
                    else
                    {
                        DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTabOld
                           = m_FeeBGNoContractNoAndFacTab.GetModel(nID);
                        modelBGNoContractNoAndFacTabOld.ShouldPayMonth = nYearMon;

                        m_FeeBGNoContractNoAndFacTab.TransUpdate(modelBGNoContractNoAndFacTabOld
                            , m_SQLStringList, m_arrayList, i);
                    }

                    CurrentUGR.Cells["应收费用计提年月"].Value = nYearMon.ToString();
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改计提年月", nYearMon.ToString());
                MessageBox.Show("批量修改应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void 清空该条记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要清空该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                bRet = IsFeeTruePayLocked(CurrentUGR);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ResetUltraGridRow(nIndex);

                int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells["FeeID"].Value.ToString(), 10);

                Boolean bNoContract = CurrentUGR.Cells["合同编号"].Value.ToString() != "";
                ResetRecord(nFeeSJTruePayTabID, bNoContract);
                m_bClearing = true;
            }
            else
            {
                MessageBox.Show("请选中要清空的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
         

            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    if (ugr[i].Cells["合同编号"].Value.ToString() != "")
                    {
                        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                                            = m_bllFeeBGTruePayTab.GetModel(nID);
                        modelFeeBGTruePayTabOld.ShouldPayMonth = 0;

                        m_bllFeeBGTruePayTab.TransUpdate(modelFeeBGTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);
                    }
                    else
                    {
                        DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTabOld
                           = m_FeeBGNoContractNoAndFacTab.GetModel(nID);
                        modelBGNoContractNoAndFacTabOld.ShouldPayMonth = 0;

                        m_FeeBGNoContractNoAndFacTab.TransUpdate(modelBGNoContractNoAndFacTabOld
                             , m_SQLStringList, m_arrayList, i);
                    }

                    CurrentUGR.Cells["应收费用计提年月"].Value = "0";
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空计提年月", "");
                MessageBox.Show("批量置空应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private bool IsLiuShuiNo(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[0-9]{3}$");
        }
        private bool IsLiuShuiNo(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return false;
            }
            else if (!IsLiuShuiNo(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须001-999之间的流水号");
                return false;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return true;
            }
        }
        private void buttonBatchModFeeNo_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                if (!IsLiuShuiNo(this.errorProvider1, this.textBoxLiuShuiNo))
                    return;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的费用编号么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);

                    if (ugr[i].Cells["合同编号"].Value.ToString() != "")
                    {
                        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                                            = m_bllFeeBGTruePayTab.GetModel(nID);

                        DateTime? dtTruePayOld = modelFeeBGTruePayTabOld.TruePayDate;
                        if (dtTruePayOld != null)
                        {
                            MessageBox.Show("原记录实付日期不为空，不能修改，若要修改，请先置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        modelFeeBGTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                        UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                        if (((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0) || ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0))
                        {
                            if ((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                      , ugr[0].Cells["经营单位"].Value.ToString()
                      , "私"
                       , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                       );

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                                modelFeeBGTruePayTabOld.ToPrivateBianHao = strRet;
                                CurrentUGR.Cells["对私费用编号"].Value = strRet;
                            }

                            if ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                      , ugr[0].Cells["经营单位"].Value.ToString()
                      , "公"
                       , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                       );

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                                modelFeeBGTruePayTabOld.ToPubBianHao = strRet;
                                CurrentUGR.Cells["对公费用编号"].Value = strRet;
                            }

                            modelFeeBGTruePayTabOld.bitFiledReserv2 = true;//编号是否手动生成
                            m_bllFeeBGTruePayTab.TransUpdate(modelFeeBGTruePayTabOld
                                , m_SQLStringList, m_arrayList, i);

                            CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                            CurrentUGR.Cells["编号是否手动生成"].Value = true;
                        }
                    }
                    else
                    {
                        DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTabOld
                           = m_FeeBGNoContractNoAndFacTab.GetModel(nID);

                        DateTime? dtTruePayOld = modelBGNoContractNoAndFacTabOld.TruePayDate;
                        if (dtTruePayOld != null)
                        {
                            MessageBox.Show("原记录实付日期不为空，不能修改，若要修改，请先置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        modelBGNoContractNoAndFacTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                        UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                        if (((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0) || ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0))
                        {
                            if ((Decimal)CurrentUGR.Cells["对私实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                      , ugr[0].Cells["经营单位"].Value.ToString()
                      , "私"
                       , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                       );

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                                modelBGNoContractNoAndFacTabOld.ToPrivateBianHao = strRet;
                                CurrentUGR.Cells["对私费用编号"].Value = strRet;
                            }

                            if ((Decimal)CurrentUGR.Cells["对公实付金额"].Value > 0)
                            {
                                string strBianHao = MakeBGBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                      , ugr[0].Cells["经营单位"].Value.ToString()
                      , "公"
                       , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                       );

                                string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                                string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                                modelBGNoContractNoAndFacTabOld.ToPubBianHao = strRet;
                                CurrentUGR.Cells["对公费用编号"].Value = strRet;
                            }

                            modelBGNoContractNoAndFacTabOld.bNoManualGeneration = true;//编号是否手动生成
                            m_FeeBGNoContractNoAndFacTab.TransUpdate(modelBGNoContractNoAndFacTabOld
                                , m_SQLStringList, m_arrayList, i);

                            CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                            CurrentUGR.Cells["编号是否手动生成"].Value = true;
                        }
                    }
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改费用编号", this.textBoxLiuShuiNo.Text);
                MessageBox.Show("批量修改费用编号完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void buttonBatchModRecvMoneyP_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();



                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    if (ugr[i].Cells["合同编号"].Value.ToString() != "")
                    {
                        DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
                                            = m_bllFeeBGTruePayTab.GetModel(nID);
                        modelFeeBGTruePayTabOld.RecvMonyePerson = this.comboBoxRecvPerson.Text;
                        m_bllFeeBGTruePayTab.TransUpdate(modelFeeBGTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);
                    }
                    else
                    {
                        DGBY.Model.FeeBGNoContractNoAndFacTab modelBGNoContractNoAndFacTabOld
                            = m_FeeBGNoContractNoAndFacTab.GetModel(nID);
                        modelBGNoContractNoAndFacTabOld.PriRecvMonyePerson = this.comboBoxRecvPerson.Text;

                        m_FeeBGNoContractNoAndFacTab.TransUpdate(modelBGNoContractNoAndFacTabOld
                             , m_SQLStringList, m_arrayList, i);
                    }

                    CurrentUGR.Cells["对私收款人"].Value = this.comboBoxRecvPerson.Text;
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改收款人", this.comboBoxRecvPerson.Text);
                MessageBox.Show("批量修改收款人完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 增加无合同号工厂记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_InsertType = InsertType.WithoutContractNoAndFac;
            UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
        }

        private void ultraGrid1_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
        {
            string strTColumn = this.ultraGrid1.ActiveCell.Column.Header.Caption;
            if (strTColumn == "转关报关行" || strTColumn == "转关报关港")
            {
                return;
            }


            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;

            int nPaidanID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells["PaidanID"].Value);
            if (nPaidanID > 0)
            {
                Frm_Main.m_nContractSelectValue = nPaidanID;
                Frm_Main.GetMainForm().ShowContent("报关-对帐版");
            }
        }
    }
}
